﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace mjRegister
{
    public class HttpProc
    {
        // Fields
        private CookieCollection _cookieGet;
        private CookieCollection _cookiePost;
        private Encoding _encoding;
        private IWebProxy _Proxy;
        private string _ResHtml;
        private string _strCode;
        private string _strErr;
        private string _strPostdata;
        private string _strRefUrl;
        private string _strUrl;
        private bool _succeed;
        const int Max_Retry_Num = 8;

        private static ConfigData configData = null;//ConfigHelper.ReadGlobalConfigData(string.Empty);
        // Methods
        public HttpProc() : this(null,null,null)
        {

        }

        public HttpProc(string 地址) : this(地址,null,null)
        {

        }

        public HttpProc(string 地址, CookieCollection 要发送的cookie) : this(地址,null,要发送的cookie)
        {

        }

        public HttpProc(string 地址, string 发送数据) : this(地址,发送数据,null)
        {

        }

        public HttpProc(string 地址, string 发送数据, CookieCollection 要发送的cookie)
        {
            if (configData == null)
            {
                configData = ConfigHelper.ReadGlobalConfigData(Path.Combine(Common.GetExeDirectory(), "config.txt"));
            }
            this._encoding = Encoding.UTF8;
            this._strUrl = 地址;
            this._strPostdata = 发送数据;
            this._cookiePost = 要发送的cookie;
        }

        public void SetEncoding(Encoding encoding)
        {
            this._encoding = encoding;
        }

        public HttpWebRequest CreateRequest()
        {
            HttpWebRequest 请求 = null;
            请求 = (HttpWebRequest)WebRequest.Create(this._strUrl);
            请求.Accept = "*/*";
            请求.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
            请求.CookieContainer = new CookieContainer();
            请求.Referer = this.strRefUrl;
            WebProxy myProxy = new WebProxy();
            if (configData != null && !string.IsNullOrEmpty(configData.ProxyServer) && configData.ProxyPort != 0)
            {
                myProxy.Address = new Uri(string.Format("http://{0}:{1}", configData.ProxyServer, configData.ProxyPort));
                if ((!string.IsNullOrEmpty(configData.ProxyUser) && !string.IsNullOrEmpty(configData.ProxyPass)))
                {
                    myProxy.Credentials = new NetworkCredential(configData.ProxyUser, configData.ProxyPass);
                }
                请求.Proxy = myProxy;
            }
            if (this._cookiePost != null)
            {
                Uri u = new Uri(this._strUrl);
                foreach (Cookie c in this._cookiePost)
                {
                    c.Domain = u.Host;
                }
                请求.CookieContainer.Add(this._cookiePost);
            }
            if ((this._strPostdata != null) && (this._strPostdata.Length > 0))
            {
                请求.ContentType = "application/x-www-form-urlencoded";
                请求.Method = "POST";
                byte[] b = this._encoding.GetBytes(this._strPostdata);
                请求.ContentLength = b.Length;
                Stream sw = null;
                try
                {
                    sw = 请求.GetRequestStream();
                    sw.Write(b, 0, b.Length);
                }
                catch (Exception ex)
                {
                    this._strErr = ex.Message;
                }
                finally
                {
                    if (sw != null)
                    {
                        sw.Close();
                    }
                }
            }
            return 请求;
        }

        public string Proc()
        {
            int retrynum = 0;
retry:      HttpWebRequest 请求 = this.CreateRequest();
            HttpWebResponse 响应 = null;
            StreamReader sr = null;
            try
            {
                响应 = (HttpWebResponse)请求.GetResponse();
                sr = new StreamReader(响应.GetResponseStream(), this.encoding);
                this._ResHtml = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                this._strErr = ex.Message;
                return "";
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
            }
            this._strCode = 响应.StatusCode.ToString();
            if (this._strCode == "302")
            {
                this._ResHtml = 响应.Headers["location"];
            }
            if (响应.Cookies.Count > 0)
            {
                this._cookieGet = 响应.Cookies;
            }
            if (this.ResHtml == string.Empty)
            {
                if (retrynum++ < Max_Retry_Num)
                {
                    goto retry;
                }
            }
            return this.ResHtml;
        }

        // Properties
        public CookieCollection cookieGet
        {
            get
            {
                return this._cookieGet;
            }
        }

        public CookieCollection cookiePost
        {
            get
            {
                return this._cookiePost;
            }
            set
            {
                this._cookiePost = value;
            }
        }

        public Encoding encoding
        {
            get
            {
                return this._encoding;
            }
            set
            {
                this._encoding = value;
            }
        }

        public IWebProxy Proxy
        {
            get
            {
                return this._Proxy;
            }
            set
            {
                this._Proxy = value;
            }
        }

        public string ResHtml
        {
            get
            {
                return this._ResHtml;
            }
        }

        public string strCode
        {
            get
            {
                return this._strCode;
            }
            set
            {
                this._strCode = value;
            }
        }

        public string strErr
        {
            get
            {
                return this._strErr;
            }
            set
            {
                this._strErr = value;
            }
        }

        public string strPostdata
        {
            get
            {
                return this._strPostdata;
            }
            set
            {
                this._strPostdata = value;
            }
        }

        public string strRefUrl
        {
            get
            {
                return this._strRefUrl;
            }
            set
            {
                this._strRefUrl = value;
            }
        }

        public string strUrl
        {
            get
            {
                return this._strUrl;
            }
            set
            {
                this._strUrl = value;
            }
        }

        public bool succeed
        {
            get
            {
                return this._succeed;
            }
            set
            {
                this._succeed = value;
            }
        }
    }
 
}
